package com.yuejian.neusoft.yuejian_24.model;

import com.yuejian.neusoft.yuejian_24.javaBean.Search;
import com.yuejian.neusoft.yuejian_24.listener.ListListener;
import com.yuejian.neusoft.yuejian_24.service.SearchService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by q4549 on 2017/11/13.
 */

public class SearchModel {
    private Retrofit retrofit;
    public SearchModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl("http://10.0.2.2/yuejian/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void search(String content, final ListListener listener){
        SearchService searchService = retrofit.create(SearchService.class);
        Call call = searchService.search(content);
        Callback<List<Search>> callback = new Callback<List<Search>>() {
            @Override
            public void onResponse(Call<List<Search>> call, Response<List<Search>> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<List<Search>> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }
}

